//
//  ST_PersonItemCell.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/25.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import HandyJSON

class ST_PersonItemCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        self.contentView.backgroundColor = Color.random
        
        contentView.addSubview(gradient)
        gradient.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 4, left: 4, bottom: 4, right: 4))
        }
        
        self.contentView.addSubview(maskLayer)
        maskLayer.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(imageView)
            make.height.equalTo(100)
        }
        
        contentView.addSubview(user_info_describe_label)
        user_info_describe_label.snp.makeConstraints { (make) in
            make.left.equalTo(imageView).offset(3)
            make.bottom.equalTo(imageView).offset(-10)
        }
        
        contentView.addSubview(nickname_label)
        nickname_label.snp.makeConstraints { (make) in
            make.left.equalTo(imageView).offset(3)
            make.bottom.equalTo(user_info_describe_label.snp.top).offset(-5)
        }
        
        contentView.addSubview(love_btn)
        love_btn.snp.makeConstraints { (make) in
            make.right.equalTo(imageView).offset(-10)
            make.bottom.equalTo(imageView).offset(-10)
        }
        
        contentView.addSubview(same_city_tag_image_view)
        same_city_tag_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(4)
            make.top.equalToSuperview().offset(14)
            make.width.equalTo(54)
            make.height.equalTo(26)
        }
        
        self.cornerRadius = 5
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//
//        let height = self.frame.height/220.0*100
//        maskLayer.frame = CGRect.init(x: 0, y: self.frame.height - height, width: self.frame.width, height: height)
//    }
    
    fileprivate lazy var same_city_tag_image_view:UIImageView = {
       let control = UIImageView()
        control.image = ST_ImageUtils.imageName("喜欢_img_同城")
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var gradient:ST_FWGradientView = {
        let control = ST_FWGradientView()
        
        control.bgLayer.colors = [0x3BF5FF,0x6561F0,0xDA42C4,0xFFEE2E].map({ (c) -> CGColor in
            return UIColor.init(hex: c)!.cgColor
        })
        control.bgLayer.locations = [0,0.33,0.66,1]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 1, y: 1)
        control.cornerRadius = 5
        control.isHidden = true
        return control
    }()

    
    lazy var imageView:UIImageView = {
       let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 5
        return control
    }()
    
    lazy var nickname_label:UILabel = {
       let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        
        return control
    }()
    
    
    lazy var user_info_describe_label:UILabel = {
        let control = UILabel()
        control.textColor = Color.white
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    lazy var love_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//        control.backgroundColor = Color.white
//        control.cornerRadius = 42/2.0
//        control.borderColor = Color.init(white: 0, alpha: 0.1)
//        control.borderWidth = 1
//        control.setTitleForAllStates("来一起拍视频啦")
//        control.setTitleColor(UIColor.gray, for: UIControl.State.normal)
        control.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_未喜欢")!)
        
        control.setImage(ST_ImageUtils.imageName("消息_icon_喜欢"), for: UIControl.State.selected)
        
        control.addTarget(self, action: #selector(love_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    
    
    // fillCode
    lazy var maskLayer:ST_FWGradientView = {
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(white: 1, alpha: 0).cgColor,UIColor.init(white: 0, alpha: 0.8).cgColor]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        return control
    }()
    
    
    
    var model:ST_LoveMeModel?{
        didSet{
            
            guard let m = model else { return  }
            
            
            imageView.pin_setImage(from: m.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            nickname_label.text = m.nickname
            
            if let sex = ST_SEX.init(rawValue: m.gender){
                let desc = String.init(format: "%d / %@ / %@", m.age,sex.sexStr(),m.constellation)
                
                print("---99--,",desc)
                
                user_info_describe_label.text = desc
            }
            
            love_btn.isSelected = m.loved
            
            gradient.isHidden = !m.is_same_city
            same_city_tag_image_view.isHidden = !m.is_same_city
            
            if m.is_same_city{
                love_btn.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_超级喜欢")!)
            }else{
                love_btn.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_未喜欢")!)
                
                love_btn.setImage(ST_ImageUtils.imageName("消息_icon_喜欢"), for: UIControl.State.selected)
            }
            
        }
    }
    
//    MARK: - Event
    var love_closure:((ST_LoveMeModel)->())?
    @objc func love_handle(){
        
        
        
        if let m = self.model,self.love_btn.isSelected == false{
            self.love_closure?(m)
        }
        
        
    }
}


struct ST_LoveMeModel:HandyJSON {
    
    
    var  home_cover:String = ""
    var  nickname:String = ""
    var  avatar:String = ""
    var  constellation:String = ""
    var  member_level:Int = 0
    var  age:Int = 18
    var  gender:Int = 0
    var  accid:String = ""
    var loved:Bool = false
    var is_same_city = false
}
